//
//  TopicObj.m
//  FbeeAPP
//
//  Created by dev-m on 2018/3/7.
//  Copyright © 2018年 mohao. All rights reserved.
//

#import "TopicObj.h"
#import "UrlLabel.h"
#import "RegexKitLite.h"

@implementation TopicObj

-(void)setObjByDict:(NSDictionary *)dict
{
    self.height = 0;
    
    self.nickname = SafeGetStringValue([dict objectForKey:@"nickname"]);
    self.content = SafeGetStringValue([dict objectForKey:@"content"]);
    self.groupId = SafeGetStringValue([dict objectForKey:@"groupId"]);
    self.userId = SafeGetStringValue([dict objectForKey:@"userId"]);
    self.topicId = SafeGetStringValue([dict objectForKey:@"id"]);
    self.commentNum = SafeGetStringValue([dict objectForKey:@"commentNum"]);
    self.shareNum = SafeGetStringValue([dict objectForKey:@"shareNum"]);
    self.isTop = SafeGetBOOLValue([dict objectForKey:@"recommendTag"]);
    
    if ([self.commentNum isEqualToString:@"0"]) {
        self.commentNum = NSLocalizedString(@"mComment", nil);
    }
    if ([self.shareNum isEqualToString:@"0"]) {
        self.shareNum = NSLocalizedString(@"mShare", nil);
    }
    
    NSTimeInterval time = SafeGetDoubleValue([dict objectForKey:@"createTime"])/1000.0f;
    NSDate *oDate = [NSDate dateWithTimeIntervalSince1970:time];
    NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
    NSTimeZone* destinationTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
    NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:oDate];
    NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:oDate];
    NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
    NSDate* currentDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:oDate];
    

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];// 创建一个时间格式化对象
    [dateFormatter setDateFormat:@"MM.dd hh:mm"];//设定时间格式,这里可以设置成自己需要的格式
    self.createTime = [dateFormatter stringFromDate:currentDate];//将时间转化成字符串
    
    self.contentLab = [[UrlLabel alloc] initWithFrame:CGRectMake(0, 0, IPHONEWIDTH - 60 - 20, 100*15)];
    self.contentLab.numberOfLines = 6;
    
    NSString *contentText = [self.content stringByReplacingOccurrencesOfRegex:@"\n+" withString:@"\n"];
    contentText = [contentText stringByReplacingOccurrencesOfRegex:@"\r+" withString:@"\n"];
    
    [self.contentLab setText:contentText textColor:FBEE_COLOR_TEXT font:[UIFont systemFontOfSize:15.0f] urlColor:FBEE_COLOR_BLUE urlTapBlock:^(NSURL *url) {
        
        UIApplication *application = [UIApplication sharedApplication];
        if ([application respondsToSelector:@selector(openURL:options:completionHandler:)]) {
            [application openURL:url options:@{} completionHandler:nil];
        } else {
            [application openURL:url];
        }
    }];
    [self.contentLab sizeToFit];
    
    
    self.contentH = self.contentLab.height;
    
    if ( self.contentH > 105 ) {
        self.contentH = 105;
        //超出限制    30
        self.height += 30;
        self.isShowAll = YES;
    }
    
    /*
        话题模块高度计算
     
        边距       10
        正文上部    44
        正文下部    46
     */
    self.height += 10 + 44 + 46 + self.contentH;
}

@end
